/*------------------------------------------------------------------------------*\
**   Copyright 2008-2009 Jonhnny Weslley                                        **
**                                                                              **
**   Licensed under the Apache License, Version 2.0 (the "License");            **
**   you may not use this file except in compliance with the License.           **
**   You may obtain a copy of the License at                                    **
**                                                                              **
**       http://www.apache.org/licenses/LICENSE-2.0                             **
**                                                                              **
**   Unless required by applicable law or agreed to in writing, software        **
**   distributed under the License is distributed on an "AS IS" BASIS,          **
**   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   **
**   See the License for the specific language governing permissions and        **
**   limitations under the License.                                             **
\*------------------------------------------------------------------------------*/

package org.scalashell.scalash
 
import scala.tools.nsc._
import org.scalashell.ShellSystem.{messages => Message}
import org.scalashell.commands.HelpCommand
import commands._

object Main {

  def welcome {
    println(Message("project.banner"))
  }

  def main(args: Array[String]) {
    ShellSystem.addMessageSource("org.scalashell.scalash.messages")

    def error1(msg: String) { println("scala: " + msg) }
    val command = new InterpreterCommand(List.fromArray(args), error1)

    if (!command.ok || command.settings.help.value || command.settings.Xhelp.value) {
      // either the command line is wrong, or the user
      // explicitly requested a help listing
      if (command.settings.help.value) {
        println(command.usageMsg)
      }
      if (command.settings.Xhelp.value) println(command.xusageMsg)
      Console.flush

    } else {
      val interactiveShell = new ScalaInteractiveShell
      val shell = new ScalaShell(command.settings, interactiveShell)

      ShellSystem.addCommand(new HelpCommand(":help"))
      ShellSystem.addCommand(new HistoryCommand(shell, interactiveShell.history))
      ShellSystem.addCommand(new LoadCommand(shell))

      welcome
      shell.run
    }
  }

}
